#! /usr/bin/python
#
# Stephen Poletto (spoletto)
# Peter Wilmot (pbwilmot)
# CSCI1580 - Web Search
# Spring 2011 - Brown University
#
# Models a document and the associated
# vector of features.

# The on disk representation for a doc
# vector is : 0 9 1:1 2:1 3:2 4:1 6:1 7:1 

import numpy

class DocumentVector:
        
    def __init__(self, onDiskRepresentation, featureCount):
        diskContents = onDiskRepresentation.split(' ')
        self.docID = int(diskContents[0])
        self.featureCount = featureCount
        self.vectorMagnitude = float(diskContents[1])
        self.featureIDToNumOccurrences = {}
        for featureCountString in diskContents[2:]:
            featureContents = featureCountString.split(':')
            featureID = int(featureContents[0])
            numOccurrences = int(featureContents[1])
            self.featureIDToNumOccurrences[featureID] = numOccurrences
            
    def normalizedVector(self):
        normalizedVector = numpy.array([0.0] * self.featureCount)
        for featureID in self.featureIDToNumOccurrences:
            numOccurrences = self.featureIDToNumOccurrences[featureID]
            normalizedVector[featureID] = numOccurrences / self.vectorMagnitude
        return normalizedVector
        
    def normalizedTFIDFVector(self, logOfFeatureIDToIDF):
        tf = self.countVector()
        weightVector = numpy.array([0.0] * self.featureCount)
        weightedResult = (1.0 + numpy.log10(tf)) * logOfFeatureIDToIDF
        numpy.putmask(weightVector, tf > 0.0, weightedResult)
        if numpy.sum(weightVector) != 0:
            weightVector = weightVector / numpy.linalg.norm(weightVector)
        return weightVector
        
    def countVector(self):
        countVector = numpy.array([0.0] * self.featureCount)
        for featureID in self.featureIDToNumOccurrences:
            countVector[featureID] = self.featureIDToNumOccurrences[featureID]
        return countVector
